package com.jl.cloud.commom.config;


import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.apis.*;
import org.apache.rocketmq.client.apis.producer.Producer;
import org.apache.rocketmq.client.apis.producer.ProducerBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
@ConditionalOnExpression("${config.rocketmq.enabled:false}")
public class RocketMQConfig {


    private String ENDPOINTS = "127.0.0.1:8094";
    private String ACCESS_KEY = "";
    private String SECRET_KEY = "";


    @Bean(name = "producer")
    public Producer getProducer() {
        ClientServiceProvider provider = ClientServiceProvider.loadService();
        SessionCredentialsProvider sessionCredentialsProvider =
                new StaticSessionCredentialsProvider(ACCESS_KEY, SECRET_KEY);
        ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder()
                .setEndpoints(ENDPOINTS)
                // On some Windows platforms, you may encounter SSL compatibility issues. Try turning off the SSL option in
                // client configuration to solve the problem please if SSL is not essential.
                // .enableSsl(false)
                .setCredentialProvider(sessionCredentialsProvider)
                .build();
        final ProducerBuilder builder = provider.newProducerBuilder()
                .setClientConfiguration(clientConfiguration);
        try {
                return  builder.build();
        } catch (ClientException e) {
            log.error("初始化Rocketmq失败生产者",e);
            throw new RuntimeException(e);
        }
    }


}
